package com.manager.plat.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.manager.plat.dao.IRouterDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.GatewayVo;
import com.manager.plat.vo.RouterVo;
/**
 * 渠道路由数据实现层
 * @author liugeng
 *
 */
public class RouterDaoImpl extends JdbcDaoSupport implements IRouterDao {
	private static Logger logger = Logger.getLogger(RouterDaoImpl.class);
	/**获得所有渠道路由配置信息列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<RouterVo> getRouterList() {
		String sql = "select id, name, detail, gateway_id, sp_number, sync_url, lower_id from tb_ivr_router where status = 1";
		logger.info(String.format("获得所有渠道路由配置信息列表[%s]", sql));
		
		List<RouterVo> routerVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(routerVoList != null && routerVoList.size() > 0){
			return routerVoList;
		}
		
		return null;
	}
	/**根据Id获得渠道路由配置信息*/
	@SuppressWarnings("unchecked")
	@Override
	public RouterVo getRouterById(int id) {
		String sql = "select id, name, detail, gateway_id, sp_number, sync_url, lower_id from tb_ivr_router where id = ? and status = 1";
		Object[] param = new Object[]{id};
		logger.info(String.format("根据Id获得渠道路由配置信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<RouterVo> routerVoList = this.getJdbcTemplate().query(sql, param, rowMapper);
		if(routerVoList != null && routerVoList.size() > 0){
			return routerVoList.get(0);
		}
		return null;
	}
	/**数据转换*/
	private ParameterizedRowMapper<RouterVo> rowMapper = new ParameterizedRowMapper<RouterVo>() {
		@Override
		public RouterVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			RouterVo routerVo = new RouterVo();
			routerVo.setId(rs.getInt("id"));
			routerVo.setName(rs.getString("name"));
			routerVo.setDetail(rs.getString("detail"));
			routerVo.setGateway_id(rs.getInt("gateway_id"));
			routerVo.setSp_number(rs.getString("sp_number"));
			routerVo.setSync_url(rs.getString("sync_url"));
			routerVo.setLower_id(rs.getInt("lower_id"));
			return routerVo;
		}
	};
	/**新增渠道路由配置信息*/
	@Override
	public int insertRouter(RouterVo routerVo) {
		String sql = "insert tb_ivr_router(name, detail, gateway_id, sp_number, sync_url, lower_id, status, add_user) values(?,?,?,?,?,?,1,?)";
		Object[] param = new Object[]{routerVo.getName(), routerVo.getDetail(),
									routerVo.getGateway_id(),routerVo.getSp_number(),
									routerVo.getSync_url(),routerVo.getLower_id(), 
									routerVo.getAdd_user()};
		
		logger.info(String.format("新增渠道路由配置信息[%s]", BaseUtil.logSQL(sql, param)));
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**更新渠道路由配置信息*/
	@Override
	public int updateRouter(RouterVo routerVo) {
		String sql = "update tb_ivr_router set name = ?, detail = ?, gateway_id = ?, sp_number = ?,  sync_url = ?, lower_id = ?, update_user = ?, update_date = now() where id = ?";
		Object[] param = new Object[]{routerVo.getName(), routerVo.getDetail(),
									routerVo.getGateway_id(),routerVo.getSp_number(),
									routerVo.getSync_url(),routerVo.getLower_id(), 
									routerVo.getUpdate_user(), routerVo.getId()};
		logger.info(String.format("更新渠道路由配置信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**删除渠道路由配置信息*/
	@Override
	public int deleteRouter(int id, int update_user) {
		String sql = "update tb_ivr_router set status = 0, update_user = ?, update_date = now() where id = ?";
		Object[] param = new Object[]{update_user, id};
		logger.info(String.format("删除渠道路由配置信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**查询渠道长号码信息列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<GatewayVo> getLowerGatewayByUserId(int userId) {
		String sql = "select gateway_id, sp_number from tb_ivr_router r " +
					 "inner join tb_ivr_partner_lower pl on pl.id = r.lower_id " +
					 "where pl.user_id = ? " +
					 "group by sp_number";
		Object[] param = new Object[]{userId};
		logger.info(String.format("查询渠道长号码信息列表[%s]", BaseUtil.logSQL(sql, param)));
		
		List<GatewayVo> gatewayVoList = this.getJdbcTemplate().query(sql, param, tempMapper);
		if(gatewayVoList != null && gatewayVoList.size() > 0){
			return gatewayVoList;
		}
		
		return null;
	}
	/**数据转换*/
	private ParameterizedRowMapper<GatewayVo> tempMapper = new ParameterizedRowMapper<GatewayVo>() {
		@Override
		public GatewayVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			GatewayVo gatewayVo = new GatewayVo();
			gatewayVo.setId(rs.getInt("gateway_id"));
			gatewayVo.setSp_number(rs.getString("sp_number"));
			return gatewayVo;
		}
	};
}
